﻿using AutoMapper;
using B.W.traceability.Domain.养殖;
using B.W.traceability.ErrorCode;
using B.W.traceability.Infrastructure;
using B.W.traceability.Wirte.API.APPlication.TraceabilitCommand;
using MediatR;

namespace B.W.traceability.Wirte.API.APPlication.TraceabilitHandler
{
    public class TraceabilitAddHandler : IRequestHandler<TraceabilitAddCommand, APIResult<object>>
    {
        private readonly IBInfrastructure<Traceabilitytable> TraceabilitytableAPIResult;
        private readonly IMapper mapper;

        public TraceabilitAddHandler(IBInfrastructure<Traceabilitytable> traceabilitytableAPIResult, IMapper mapper)
        {
            TraceabilitytableAPIResult = traceabilitytableAPIResult;
            this.mapper = mapper;
        }

        public Task<APIResult<object>> Handle(TraceabilitAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            DateTime dateTime = DateTime.Now;
            string ComaindateTime = dateTime.ToString("yyyyMMdd");
            string dateTimestring = ComaindateTime;

            result.Code = ErrorAPIResult.添加成功;
            result.Msg = ErrorAPIResult.添加成功.ToString();
            var res = mapper.Map<Traceabilitytable>(request);
          
          
            TraceabilitytableAPIResult.Add(res);
            res.TraceabilityDateTime = dateTimestring;
            result.Data =res;
            return Task.FromResult(result);
        }
    }
}
